package com.remote.filter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;

@Component
public class LoginInterceptor implements HandlerInterceptor {
	/**
     * 预处理回调方法，实现处理器的预处理
     * 返回值：true表示继续流程；false表示流程中断，不会继续调用其他的拦截器或处理器
   */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        System.out.println("开始拦截.........");
        //业务代码
        HttpSession httpSession=request.getSession();
        Object connInfoSession = httpSession.getAttribute("connInfo");
		if(connInfoSession==null) {
			StringBuffer RequestURL = request.getRequestURL();
			String QueryString=request.getQueryString();
			if(!StringUtils.isEmpty(QueryString)) {
				RequestURL.append("?").append(QueryString);
			}
			System.out.println(RequestURL);
			request.setAttribute("RequestURL", RequestURL.toString());
			request.getRequestDispatcher("/").forward(request, response);
			return false;
		}
        return true;
    }

}